//
//  MWFeedBackVC.m
//  CarCare
//
//  Created by meway on 14-7-28.
//  Copyright (c) 2014年 baozun. All rights reserved.
//
 
#import "MWFeedBackVC.h"
#import "NetHome.h"

#define SIDE_GAP 20

@interface MWFeedBackVC ()
<UITextViewDelegate>
@property (strong, nonatomic) UITextView *textView;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UILabel *label;

@property (nonatomic, strong) UIButton *okButton;

@end

@implementation MWFeedBackVC

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	
	self.title = @"意见反馈";
	
    self.textView = [[UITextView alloc]initWithFrame:CGRectMake(SIDE_GAP, SIDE_GAP, self.view.width - 2 * SIDE_GAP, 200)];
    //	self.textView.layer.cornerRadius = 6;
	self.textView.delegate = self;
    self.textView.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:self.textView];
    [self.textView becomeFirstResponder];

    
    self.label = [Factory labelWithFrame:CGRectMake(0, self.textView.bottom, self.view.width, 40) font:[UIFont systemFontOfSize:12] text:LOCSTRING(@"tip_feedback") textColor:[UIColor lightGrayColor] onView:self.view textAlignment:NSTextAlignmentCenter];
    
    __weak __typeof(self) wSelf = self;

    self.okButton = [ViewFactory normalButtonWithFrame:FactoryRectButtonDefault(__MAIN_HEIGHT_64 - BOTTOM_SIDE) title:LOCSTRING(@"send") inView:self.view action:^{
        [wSelf clickOnOK];
    }];
    
    self.label.bottom = self.okButton.top - 10;
    self.textView.height = self.label.top - SIDE_GAP - 20;
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
//    [self.textView becomeFirstResponder];
}
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [super clickOnBG];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)clickOnOK{
    if ([Check isNoNull:self.textView.text]) {
        [self.view endEditing:YES];
        [[[NetHome feedBackWithMessage:self.textView.text] requestWithLoad:RequestLoadShowLoading|RequestLoadShowErrorTips] requestSuccess:^(id JSON) {
            TipsView *tip = [TipsView okTipsWithTitle:LOCSTRING(@"feedback_succ") lightTitles:@[LOCSTRING(@"tip_feedback")]];
            [tip addOKButton:LOCSTRING(@"ok") clickOK:^{
                [AlertMgr dismiss:TYPE_VIEW_ALERT];
//                [self.navigationController popViewControllerAnimated:YES];
                [SCREEN showMainScreen];
            }];
            
            [AlertMgr showView:tip];
        }];
    }
}

#pragma mark - UITextViewDelegate

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    BOOL hasNull = NO;
    if (![text isNotNil] && textView.text.length <= 1) {
        hasNull = YES;
    }
    self.okButton.enabled = !hasNull;
	return YES;
}


#pragma mark - keyboard
-(void)moveViewWhenKeyBoardChangeToHeight:(CGFloat)height WithDuiation:(NSTimeInterval)duration{

    [UIView animateWithDuration:duration animations:^{
        if (__SCREEN_IS_3_5) {
            
            CGFloat newH = height;
            if (height > 0) {
                newH = height - 100;
            }
            self.scrollView.contentSize = CGSizeMake(__MAIN_WIDTH, __MAIN_HEIGHT_64 + newH);
            
            self.okButton.bottom = __MAIN_HEIGHT_64 - ((newH == 0) ? 30 : newH + 10);
            self.label.bottom = self.okButton.top - 10;
            self.textView.height = self.label.top - SIDE_GAP - 20;
 
        }else{
            self.okButton.bottom = __MAIN_HEIGHT_64 - ((height == 0) ? 30 : height + 10);
            self.label.bottom = self.okButton.top - 10;
            self.textView.height = self.label.top - SIDE_GAP - 20;
        }
    }];

}

@end
